﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NOVO_USB2XXX_backup
{
    enum NOVO_DEV_List
    {
        CH347 = 0,
        CH341 = 1,
        DoThinkey = 2,
        USB2XXX = 3
    }
    enum NOVO_IIC_Speed
    {
        IIC_20K = 0,
        IIC_100K = 1,
        IIC_400K = 2,
        IIC_750K = 3
    }

    struct NOVO_IIC_CONFIG
    {
        public uint ClockSpeedHz;  //IIC时钟频率:单位为Hz        
        public byte EnablePu;      //使能引脚芯片内部上拉电阻，若不使能，则I2C总线上必须接上拉电阻        
    }

    struct NOVO_SPI_CONFIG
    {
        public int CSIndex;         //SPI CS选择, 只是支持0/1
        public byte CPOL;           //时钟极性控制:0-SCK空闲时为低电平，1-SCK空闲时为高电平
        public byte CPHA;           //时钟相位控制:0-第一个SCK时钟采样，1-第二个SCK时钟采样
        public byte LSBFirst;       //数据移位方式:0-MSB在前，1-LSB在前
        public byte SelPolarity;    //片选信号极性:0-低电平选中，1-高电平选中
        public uint ClockSpeedHz;   //SPI时钟频率:单位为HZ，CH347最大60M,USB2XXX最大50M，可设置速率为最大频率的2^n分频
    }

    struct NOVO_UART_CONFIG
    {
        public int Index;           //UART通道选择,只支持0/1
        public byte DataBit;        //数据位宽
        public byte Parity;         //奇偶校验位
        public byte StopBit;        //停止位
        public uint BaudRate;       //波特率设置
    }
    

    struct NOVO_DEV_CONFIG
    {
        public NOVO_DEV_List devType;
        public bool I2C_EN;    //是否打开I2C功能，FALSE时无视IIC_CONFIG
        public bool SPI_EN;    //是否打开SPI功能，FALSH时无视SPI_CONFIG
        public bool UART_EN;   //是否打开UART功能，FALSH时无视以下所有设置
        public bool GPIO_EN;   //是否打开GPIO功能，FLASH时无视GPIO Mask
    }

    public struct NOVO_DEV_INFO
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Byte[] FirmwareName;   //固件名称字符串
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Byte[] BuildDate;    //固件编译时间字符串
        public UInt32 HardwareVersion;//硬件版本号
        public UInt32 FirmwareVersion;//固件版本号
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public UInt32[] SerialNumber; //适配器序列号
        public UInt32 Functions;      //适配器当前具备的功能
    }


    class NovoComm
    {
        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_CreateSdk();

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Novo_DeleteSdk();

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_InitDev(ref NOVO_DEV_CONFIG pDevCfg);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_EnumDevice();

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_SetDeviceId(int devID);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_OpenDevice();

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_CloseDevice();

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_GetDevInfo(ref NOVO_DEV_INFO pDevInfo);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void Novo_GetDevName(StringBuilder devName);
        //public static extern void Novo_GetName(ref string devName);

        ///I2C总线
        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Init(ref NOVO_IIC_CONFIG I2CConfig);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_SetIndex(int IICIndex);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_ScanDevAddr(byte[] pSlaveAddr);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_RW_U8_Basic(byte devAddr, byte[] pWriteData, int writeLen, byte[] pReadData, int readLen);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Wr_U8(byte devAddr, byte regAddr, byte regData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Wr_U16(byte devAddr, UInt16 regAddr, UInt16 regData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Wr_U32(byte devAddr, UInt32 regAddr, UInt32 regData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Wr_U16U8(byte devAddr, UInt16 regAddr, byte regData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Wr_U16U32(byte devAddr, UInt16 regAddr, UInt32 regData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Rd_U8(byte devAddr, byte regAddr, ref byte pRegData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Rd_U16(byte devAddr, UInt16 regAddr, ref UInt16 pRegData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Rd_U32(byte devAddr, UInt32 regAddr, ref UInt32 pRegData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Rd_U16U8(byte devAddr, UInt16 regAddr, ref byte pRegData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_IIC_Rd_U16U32(byte devAddr, UInt16 regAddr, ref UInt32 pRegData);

        ///GPIO
        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_GPIO_Get(byte mask, ref byte pData);

        [DllImport("NovoComm.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 Novo_GPIO_Set(byte mask, byte data);




    }

}
